package com.apexedu.biz.webservice;

import com.apexedu.biz.entity.client.CommandResult;
import com.apexedu.biz.entity.client.ResourceForshdEntity;
import com.apexedu.biz.service.impl.AuthServiceImpl;
import com.apexedu.support.spring.SpringUtil;
import com.apexedu.util.json.JsonUtil;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import javax.jws.WebMethod;
import javax.jws.WebService;

/**
 * 对外发布的 webservice
 * User: 杨建亮
 * Date: 13-11-18  下午4:29
 */
@WebService()
public class AuthWebService {

    private static final Log LOG = LogFactory.getLog(AuthWebService.class);

    /**
     * 下载指定用户的授权资源
     * @param userLoginId 用户登录账户
     * @param clientAppId 所属系统的接入号
     * @return
     */
    @WebMethod
    public CommandResult getResourcesByAssignUser( String userLoginId, String clientAppId ) {
        AuthServiceImpl service = (AuthServiceImpl) SpringUtil.getBean("authServiceImpl");
        CommandResult result;
        try {
            result = service.getResourcesByAssignUser(clientAppId, userLoginId);
        } catch (Exception e) {
            LOG.error(e);
            result = new CommandResult();
            result.setHasError(true);
            result.setError( e.getMessage() );
        }
        return result;
    }

    /**
     * 验证指定用户的资源权限
     * @param userLoginId 用户登录账户
     * @param srcId 业务系统中的资源编号
     * @param clientAppId 所属系统的接入号
     * @return
     */
    @WebMethod
    public CommandResult checkRights( String userLoginId, String srcId, String clientAppId ) {
        AuthServiceImpl service = (AuthServiceImpl) SpringUtil.getBean("authServiceImpl");
        CommandResult result;
        try {
            result = service.checkRights(clientAppId, userLoginId, srcId);
        } catch (Exception e) {
            LOG.error(e);
            result = new CommandResult();
            result.setHasError(true); // 默认是false
            result.setError( e.getMessage() );
        }
        return result;
    }

    /**
     * 查询学生/教工基本信息
     * @param userLoginId 用户登录账户
     * @param clientAppId 所属系统的接入号
     * @return
     */
    @WebMethod
    public  String getResourcesByShdUser(String userLoginId, String clientAppId) {
        AuthServiceImpl service = (AuthServiceImpl) SpringUtil.getBean("authServiceImpl");
        ResourceForshdEntity result;
        String jsonstr="";
        try {
            result = service.getResourcesByShdUser(clientAppId, userLoginId);
            jsonstr = JsonUtil.getJSONString(result);
        } catch (Exception e) {
            LOG.error(e);
        }
        return jsonstr;
    }

}
